// pages/my/myEdit/myEdit.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showUrl: '',
    userInfo: {},
    picList: []
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  saveUserInfo() {
    let that = this;
    if (this.data.type && 1 == this.data.type * 1) {
      let d = wx.getStorageSync('userData')
      let u = this.data.userInfo;
      d.cusCompany=u.cusCompany;
      d.cusKtz = u.cusKtz;
      d.cusName = u.cusName;
      d.cusTel = u.cusTel;
      d.cusAddress = u.cusAddress;
      d.cusEmail = u.cusEmail;
      if (!d.cusName || !d.cusTel || !d.cusKtz) {
        wx.showToast({
          title: '请完善信息后继续',
          icon: 'none'
        })
        return
      }
      app.req({
        url: '/customer/busCustomer/add',
        data: d,
        method: 'post',
        success: function (res) {
          if (res.code === 200) {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }else{
            wx.showToast({
              title: '请求错误，请确认输入信息',
              duration: '2000'
            })
          }
        }
      });
    } else {
      app.req({
        url: "/customer/busCustomer/edit",
        data: that.data.userInfo,
        method: 'PUT',
        success(res) {
          if (200 == res.code) {
            app.showToast({
              title: '操作成功',
              success() {
                wx.navigateBack({
                  delta: 1,
                })
              }
            })
          }
        }
      })
    }

  },
  changeValue(e) {
    this.toggle(e.currentTarget.dataset.param, e.detail.value);
  },
  login(f, e) {
    app.getUserInfo({
      success(res) {
        f && f(res);
      },
      error() {
        e && e();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toggle("showUrl", app.globalData.showUrl);
    this.toggle("type", options.type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.login(function (res) {
      that.toggle("userInfo", res);
      console.log(res)
    }, function () {
      if (that.data.type && 1 == that.data.type * 1) {
        let d = wx.getStorageSync('userData');
        that.toggle("userInfo.cusName", d.cusName);
        that.toggle("userInfo.cusAddress", (d.country || '') + (d.province || '') + (d.city || ''));
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})